package com.mm.seguridad.service;

import com.mm.support.jpa.UserDetailsService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.Collection;

/**
 * User: aglwkrs
 * Date: 26/12/11
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {


    public UserDetails getUserDetails() {
        if (SecurityContextHolder.getContext() == null) {
            throw new IllegalStateException("Error no se puede obtener contexto seguridad usuario");
        }
        return new UserDetails() {
            public Collection<GrantedAuthority> getAuthorities() {
                return SecurityContextHolder.getContext().getAuthentication().getAuthorities();
            }

            public String getPassword() {
               return (String)SecurityContextHolder.getContext().getAuthentication().getCredentials();
            }

            public String getUsername() {
                return  (String)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            }

            public boolean isAccountNonExpired() {
                return true;  //To change body of implemented methods use File | Settings | File Templates.
            }

            public boolean isAccountNonLocked() {
                return true;  //To change body of implemented methods use File | Settings | File Templates.
            }

            public boolean isCredentialsNonExpired() {
                return true;  //To change body of implemented methods use File | Settings | File Templates.
            }

            public boolean isEnabled() {
                return SecurityContextHolder.getContext().getAuthentication().isAuthenticated();  //To change body of implemented methods use File | Settings | File Templates.
            }
        };
    }
}
